#!/bin/bash

# parameters
EOLD="${1}"
ENEW="${2}"
TYPE="${3}"
EREM="${4}"

# variables
ECLASS_DIR=eclass
ECLASS="${ECLASS_DIR}/libreoffice.eclass"
LIST="ooo.lst.in"
TAG=$(basename $ENEW .ebuild)

# types
[ -z "${TYPE}" ] && TYPE="default"

default_bump()
{
	grep -v "ADDONS_SRC" "${EOLD}" > "${ENEW}"

	[ -n "${EREM}" ] && rm -f "${EOLD}"
}

default_clean() { :; }

svn_bump()
{
	if [ -n "${EREM}" ]
	then
		svn mv "${EOLD}" "${ENEW}"
	else
		svn cp "${EOLD}" "${ENEW}"
	fi
	
	sed /ADDONS_SRC/d -i "${ENEW}"
}

svn_clean()
{
	svn revert "${EOLD}"
	svn revert "${ENEW}"
	rm -f "${ENEW}"
}

# functions
call_function()
{
	if [[ $(type -t ${1}) == function ]]
	then
		${1}
	else
		warn "function: ${1}: not found! code left in unknown state"
	fi
}

die()
{
	echo "error: ${@}"

	call_function "${TYPE}_clean"

	exit 1
}

warn()
{
	echo "warning: ${@}"
}

help()
{
	local path="app-office/libreoffice"
	cat << EOH
  Usage: $0 ${path}/<old_ebuild> ${path}/<new_ebuild> [<type>] [<remove_old>]

  Where <type> could be:
    svn   - subversion
    git   - git (not implemented)

  Set <remove_old> non-void to remove old ebuild
EOH

	exit 1
}

fetch_list ()
{
	# from "download_external_sources.sh"
	wget http://cgit.freedesktop.org/libreoffice/core/plain/${LIST}?id=${TAG} -O ${LIST}

	# remove foo
	sed -e /^$/d -e /^@/d -e /README/d -e /LICENSE/d -e /Python/d \
		-e /seamonkey/d -e /xpdf/d -e /translate-toolkit/d \
		-e /nss/d -i ${LIST}

	# update paths
	sed -e "s#http://hg.services.openoffice.org/binaries#http://dev-www.libreoffice.org/src#" \
		-e "s#http://download.go-oo.org/src#http://dev-www.libreoffice.org/src#" \
		-i ${LIST}

	return 0
}

filter_list()
{
	if [ ! -f "$LIST" ]
	then
		die "filter_list() - File $LIST does not exists"
	fi

	for package in $(egrep -v "^http|\.txt|\.html|core.zip" $LIST | cut -c 34- | cut -d "." -f 1 | sed -r -e "s/([A-Za-z]+)[0-9_-].*/\1/")
	do
		# exception: libxml2 vs libxml.jar ...
		[[ ${package} == libxml ]] && package="libxml2"

		if egrep -q -s -i -c "^[^#][^\/]*\/$package" $ECLASS
		then
			echo "Dependency found: $package"
			sed -e /-$package/d -i $LIST
		else
			echo "Dependency NOT found: $package"
		fi
	done

	return 0
}

bump_ebuild()
{
	[ -f "$ENEW" ] && rm -f "$ENEW"
	if [ -z "$EOLD" -o -z "$ENEW" -o -z "$LIST" ]
	then
		die "bump_ebuild() - missing parameters: bump_ebuild <old> <new> <list>"
	fi

	call_function "${TYPE}_bump"

	while read LINE
	do
		if [ $(echo "$LINE" |grep -c "^http") -gt 0 ]
		then
			if [[ ${LINE} == http://dev-www.libreoffice.org/src ]] ; then
				SERVER="\${ADDONS_URI}"
			else
				SERVER="$LINE"
			fi
		else
			echo "ADDONS_SRC+=\" ${SERVER}/${LINE}\"" >> $ENEW
		fi
	done < $LIST

	echo "SRC_URI+=\" \${ADDONS_SRC}\"" >> $ENEW
}

# checks
if [ -z "$EOLD" -o -z "$ENEW" ]
then
	help
fi

if [ ! -f "${ECLASS}" ]
then
	die "Eclass file does not exist: ${ECLASS}"
fi

if [ ! -f "$EOLD" ]
then
	die "Ebuild file does not exist: $EOLD"
fi

if [ -f "$ENEW" ]
then
	warn "Removing previous ebuild: $ENEW"
	warn "Waiting 5 seconds for Ctrl+C"

	sleep 5
fi

# bumping
echo "TAG: $TAG"
echo

fetch_list
filter_list
bump_ebuild

# cleanup
rm -f $LIST
